$(document).ready(function() {
	//外部js调用
    laydate({
        elem: '#startTime', //需显示日期的元素选择器
		event: 'focus', //触发事件
		format: 'YYYY-MM-DD hh:mm:ss', //日期格式
		istime: true, //是否开启时间选择
		isclear: true, //是否显示清空
		istoday: true, //是否显示今天
		issure: true, //是否显示确认
		festival: true, //是否显示节日
		min:'1900-01-01 00:00:00',//最小日期
		max:'2099-12-31 23:59:59',//最大日期
		start:laydate.now(),    //开始日期
		key:'2017-01-17 23:22:59'
		//choose: function(dates){} //选择好日期的回调
    });    
    //外部js调用
    laydate({
        elem: '#endTime', //需显示日期的元素选择器
		event: 'focus', //触发事件
		format: 'YYYY-MM-DD hh:mm:ss', //日期格式
		istime: true, //是否开启时间选择
		isclear: true, //是否显示清空
		istoday: true, //是否显示今天
		issure: true, //是否显示确认
		festival: true, //是否显示节日
		min:'1900-01-01 00:00:00',//最小日期
		max:'2099-12-31 23:59:59',//最大日期
		start:laydate.now(),    //开始日期
		key:'2017-01-17 23:22:59'
		//choose: function(dates){} //选择好日期的回调
    });
	//模糊查询
	searchEvent();
	//下拉框查询
	changeEvent();
	//初始化数据
	init(1,0);
});
//默认加载  
function init(pageNo,status){
	//获取信息列表
	var orderStatus='';
	if(status!=0){
		orderStatus=status;
	}else{
		orderStatus=getvl("status");	
	}

	if(orderStatus==1||orderStatus==-3){
		$('#show').css("display", "none");
	}
	
	if(orderStatus==-1){
		$('#statusone').css("display", "block");
		$('#time').css("display", "block");
	}
	if(orderStatus==-2){
		$('#statustwo').css("display", "block");
		$('#time').css("display", "block");
	}
	$("#thislist").html("");
	$.ajax({
		url: urlcore + "/api/order/jpa/sqlPage?pageNum="+pageNo+"&pageSize="+pageSize+"&orderStatus="+orderStatus+"&search="+$('#search').val()+"&startTime="+$('#startTime').val()+"&endTime="+$('#endTime').val(),
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				//i表示在data中的索引位置，n表示包含的信息的对象
				var status=orderStatus;
				$.each(data.data.list,function(i,n){							
					var id = n.id;				
					var orderStatus=orderStatusStr(n.status);
					//将时间戳转成时间格式
					var	gmtDatetime=new Date(data.data.list[i].time).pattern("yyyy-MM-dd hh:mm:ss");					
					if(n.mark==undefined || n.mark==null){
						n.mark="无";
					}
					if(n.ridename==undefined){
						n.ridename="无";
					}
					if(n.send==undefined){
						n.send="无";
					}

					var thislist = 
					'	<tr>'+
					'		<td>'+(i+1)+'</td>'+					
					'		<td>'+n.number+'</td>'+
					'		<td>'+gmtDatetime+'</td>'+
					'		<td>'+n.shopname+'</td>';					
					if(n.status != 1){
						thislist +=
									'		<td>'+n.ridename+'</td>';
					}
					thislist +=	
					'		<td>'+n.send+'</td>'+		
					'		<td>'+n.tip+'</td>'+	
					'		<td>'+n.changemoney+'</td>'+							
					'		<td>'+n.mark+'</td>'+						
					'		<td>'+			
					'			<a class="btn btn-primary btn-xs" href="order_details.html?id='+id+'"> 详细</a>';
					if(status==-3){
						thislist +=	"&nbsp"+"&nbsp"+"&nbsp"+'<a class="btn btn-primary btn-xs" href="sendordertoride_list.html?id='+id+'">手动指派</a>';
					}
					thislist +=
					'		</td>'+
					'	</tr>';
					$('#thislist').append(thislist);																								
				});
				$('#thiscount').text(data.data.total);
				$("#pager").pager({
					pagenumber: pageNo, 
					pagecount:data.data.pages,
					totalcount:data.data.total,
					buttonClickCallback: PageClick
				}); 
				
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				if (data.msg != '空数据') {
					alert(data.msg)
				}else{
					$('#thiscount').text(0);
				}
			}

		},
		error:function() {
			alert("error");
		}
	});
	
	$.ajax({
        url: urlcore + "/api/order/orderSum?orderStatus="+orderStatus+"&search="+$('#search').val()+"&startTime="+$('#startTime').val()+"&endTime="+$('#endTime').val(),
        type: "get",
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success:function(data){
            if (data.success == true) {
            	var da=data.data;
            	if(JSON.stringify(da)=="{}"){
	                $('#takemoney').text(0);
					$('#tip').text(0);
					$('#changemoney').text(0);
	                $('#sendmoney').text(0);
            	}else{
	                $('#takemoney').text(da.takemoney);
					$('#tip').text(da.tip);
					$('#changemoney').text(da.changemoney);
	                $('#sendmoney').text(da.sendmoney);
            	}
            } else if (data.code == 'OVERTIME'){
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href="login.html";
                }
            } else {
                alert(data.msg);
            }
        },
        error:function() {
            /* Act on the event */
            alert("error");
        }
    });
    

}

function sendOrder(id){
	$.ajax({
        url: urlcore + "/api/user/sendOrderToRiderBySystem?orderId="+id,
        type: "get",
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success:function(data){
            if (data.success == true) {
            	alert(data.msg);
                window.location.href = "order_list.html?status="+-3;                
            } else if (data.code == 'OVERTIME'){
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href="login.html";
                }
            } else {
                alert(data.msg);
            }
        },
        error:function() {
            /* Act on the event */
            alert("error");
        }
    });
}

//回调函数  
PageClick = function(pageclickednumber) {  
    init(pageclickednumber,0); 
}

/**
 * 用户状态返回
 * @param {Object} orderStatus
 */
function orderStatusStr(orderStatus){
	if (orderStatus==1) {
		orderStatus = '待抢单';
	}else if(orderStatus==2) {
		orderStatus = '待取货';
	}
	else if(orderStatus==3) {
		orderStatus = '配送中';
	}
	else if(orderStatus==4) {
		orderStatus = '已送达';
	}
	else if(orderStatus==6) {
		orderStatus = '货物损坏';
	}
	else if(orderStatus==8) {
		orderStatus = '订单未抢取消';
	}
	else if(orderStatus==9) {
		orderStatus = '已抢未取货取消';
	}
	else if(orderStatus==10) {
		orderStatus = '已配送取消';
	}
	return orderStatus;
}



/**
 * 模糊查询
 */
function searchEvent(){
	$('#search').bind('keypress',function(event){                 
         if(event.keyCode == 13) {  
              init(1,0);
         }  
    });
}

/**
 * 下拉框查询
 */
function changeEvent(){
       $("#changeone").change(function () {
       		var status=$("#changeone").val();
       		init(1,status);
   		});
   		$("#changetwo").change(function () {
       		var status=$("#changetwo").val();   			
       		init(1,status);
   		});
}
